﻿/// <reference path="jquery-1.5.1-vsdoc.js" />
var splitIndex = false;
var openWin = true;
$(function () {
    if (MVC.Action == "index1") {
        $(".btn_back").click(function () {
            if (window.parent) {
                if ($(this).attr("openurl")) {
                    window.parent.location.href = $(this).attr("openurl");
                }
            }
        });
        $(".cb_all").click(function () {
            $(".cb_item").prop("checked", $(this).prop("checked"));
        });
        $(".cb_item").click(function (event) { event.stopPropagation(); });
        $("table tr.item").each(function () {
            $(this).children("td:last").click(function (event) {
                event.stopPropagation();
            });
        });
        if (splitIndex) {
            $(".index").append($("<div class='split'></div><div class='detail'><iframe id='fmdetail' name='fmdetail' frameborder='0'></iframe></div><div class='mask'></div>"));
            $("table tr.item").click(function () {
                detail($(this).attr("i"));
                if (cur_item) {
                    $(cur_item).removeClass("cur");
                    cur_item = null;
                }
                $(this).addClass("cur");
                cur_item = this;
            });
            var _move = false;
            var _x, x;
            var leftstart, lw, rw;
            var lnw = 0;
            if ($.cookie("index_lnw") != null) {
                lnw = parseInt($.cookie("index_lnw"));
            }
            var rnw = 0;
            var minlnw = 320;
            var minrnw = 150;
            var draglw = 320;
            var dragrw = 150;
            if (lnw == 0) {
                if ($.browser.msie && $.browser.version <= 7) {
                    $(".form").width(document.documentElement.clientWidth * 0.5 - 5);
                }
                else {
                    $(".form").width(document.documentElement.clientWidth * 0.5 - 6);
                }
                $(".detail").width(document.documentElement.clientWidth * 0.5);
                lnw = $(".form").width();
                $.cookie("index_lnw", lnw);
            } else {
                $(".form").width(lnw);
                $(".detail").width(document.documentElement.clientWidth - 6 - lnw);
            }

            $(".split").mousedown(function (e) {
                _move = true;
                _x = e.pageX
                leftstart = $("#listCenter").css("left");
                lw = $(".form").width();
                rw = $(".detail").width();
                $(".split").fadeTo(20, 0.5);
                $(".mask").show();
            });
            $(document).mousemove(function (e) {
                if (_move) {
                    x = e.pageX - _x;
                    if (e.pageX > draglw && e.pageX + dragrw < document.documentElement.clientWidth) {
                        $(".split").css("left", leftstart + x);
                        $(".form").width(lw + x);
                        $(".detail").width(rw - x);
                        var height = document.documentElement.clientHeight;
                        $(".hscroll").height(height - 88);
                        if ($(".hscroll").height() + 15 <= $(".hscroll")[0].scrollHeight) {
                            $(".hscroll table").width($(".form").width() - 15);
                        }
                        else {
                            $(".hscroll table").width($(".form").width());
                        }
                        if ($.browser.msie && $.browser.version <= 7) {
                            $(".hscroll").height($(".hscroll").height() - 18);
                        }
                    }
                }
            }).mouseup(function () {
                if (_move) {
                    lnw = $(".form").width();
                    $.cookie("index_lnw", lnw);
                }
                _move = false;
                $(".split").fadeTo("fast", 1);
                $(".mask").hide();
            });
        }
        else {
            $("table tr.item").mouseover(function () {
                if (cur_item) {
                    $(cur_item).removeClass("cur");
                    cur_item = null;
                }
                $(this).addClass("cur");
                cur_item = this;
            }).click(function () {
                var feature;
                eval("feature=" + $(this).attr("winfeature"));
                OpenWindow(MVC.DetailUrl + "/" + $(this).attr("i"), feature);
            });
        }

        $("#btn_delall").click(function () {
            if ($(".cb_item:checked").length <= 0) {
                alert("请选择需要删除的记录");
                return false;
            }
            if (!confirm("确定删除？")) {
                return false;
            }
            $("#mvc_command").val("delall");
            SubmitForm();

        });
        $("#btn_new").click(function () {
            //window.open(MVC.CreateUrl, "1231", "dialogWidth=100px;dialogHeight=100px;status=no;resizable=yes");
            var createurl = $(this).attr("openurl");
            if (!createurl) {
                createurl = MVC.CreateUrl;
            }
            try {
                var feature;
                eval("feature=" + $(this).attr("winfeature"));
                OpenWindow(createurl, feature);

            } catch (e) {
                OpenWindow(createurl);
            }
            return false;
        });
        $(".btn_edit").click(function () {
            try {
                var feature;
                eval("feature=" + $(this).attr("winfeature"));
                feature.target = $(this).attr("target");
                OpenWindow($(this).attr("href"), feature);
            } catch (e) {
                var feature;
                eval("feature=" + $(this).attr("winfeature"));
                OpenWindow($(this).attr("href"), feature);
            }
            return false;
        });
        $("#btn_query").click(function () {
            $("#mvc_command").val("query");
            $("#mvc_args").val($("#query").val());
            SubmitForm();
        });
        $("#query").keypress(function (event) {
            if (event.keyCode == 13)
                $("#btn_query").triggerHandler("click");
        });

        $("#tb_goto").keypress(function (event) {
            if (event.keyCode == 13)
                $("#btn_goto").triggerHandler("click");
        });

        $(".form").show();
        $(window).resize(function () {
            if (splitIndex) {
                if (lnw == 0) {
                    lnw = parseInt($.cookie("index_lnw"));
                }
                var height = document.documentElement.clientHeight;
                var width = document.documentElement.clientWidth;
                if (width - 6 >= lnw + minrnw) {
                    $(".detail").width(width - lnw - 6);
                    $(".form").width(lnw);
                    $(".index").width(width);
                } else if (width - 6 >= minlnw + minrnw) {
                    $(".detail").width(minrnw);
                    $(".index").width(width);
                    $(".form").width(width - minrnw - 6);
                } else if (width - 6 < minlnw + minrnw) {
                    $(".index").width(minlnw + minrnw + 6);
                    $(".detail").width(minrnw);
                    $(".form").width(minlnw);
                }
                $(".detail").height(height - 1);
                $(".form").height(height - 1);
                $("#fmdetail").height(height - 1);
                $(".split").height(height - 1);
                $(".mask").height(height - 1);
                $(".hscroll").height(height - 88);
                //alert($(".hscroll").height() + " " + $(".hscroll")[0].scrollHeight);
                if ($(".hscroll").height() + 15 <= $(".hscroll")[0].scrollHeight) {
                    $(".hscroll table").width($(".form").width() - 15);
                }
                else {
                    $(".hscroll table").width($(".form").width());
                }
                if ($.browser.msie && $.browser.version <= 7) {
                    $(".hscroll").height($(".hscroll").height() - 18);
                }
            }
            else {
                var height = document.documentElement.clientHeight;
                var width = document.documentElement.clientWidth;
                $(".form").width(width);
                $(".form").height(height);
                $(".hscroll").height(height - 88);
                if ($(".hscroll").height() + 15 <= $(".hscroll")[0].scrollHeight) {
                    $(".hscroll table").width($(".form").width() - 15);
                }
                else {
                    $(".hscroll table").width($(".form").width());
                }
                if ($.browser.msie && $.browser.version <= 7) {
                    $(".hscroll").height($(".hscroll").height() - 18);
                }
            }
        }).triggerHandler("resize");
        if (splitIndex) {
            $("table tr.item:eq(0)").triggerHandler("click");
        }
    }
    else if (MVC.Action == "edit") {
        var tabs = $(".function ul li");
        $(window).resize(function () {
            var sizeheight = 0;
            if ($(".nofunction").length > 0) {
                sizeheight = 32;
            }
            $(".body").height(document.documentElement.clientHeight - 110 + sizeheight);
        }).triggerHandler("resize");
        $(tabs).click(function () {
            $(".function ul li").removeClass("cur");
            $($(".function ul li")[$(this).index()]).addClass("cur");
            $(".body ul").removeClass("cur");
            $($(".body ul")[$(this).index()]).addClass("cur");
        });
        $(".btn_new").mousemove(function () {
            var length = $(".body ul").length;
            for (var i = 0; i < length; i++) {
                if ($($(".body ul")[i]).find(".field-validation-error").length > 0) {
                    $($(".function ul li")[i]).click();
                }
            }
        });
    }
    else if (MVC.Action == "details") {
        var tabs = $(".function ul li");
        $(window).resize(function () {
            var sizeheight = 0;
            if ($(".nofunction").length > 0) {
                sizeheight = 32;
            }
            $(".body").height(document.documentElement.clientHeight - 110 + sizeheight);
        }).triggerHandler("resize");
        $(tabs).click(function () {
            $(".function ul li").removeClass("cur");
            $($(".function ul li")[$(this).index()]).addClass("cur");
            $(".body ul").removeClass("cur");
            $($(".body ul")[$(this).index()]).addClass("cur");
        });
    }
    else if (MVC.Action == "create") {
        var tabs = $(".function ul li");
        $(window).resize(function () {
            var sizeheight = 0;
            if ($(".nofunction").length > 0) {
                sizeheight = 32;
            }
            $(".body").height(document.documentElement.clientHeight - 110 + sizeheight);
        }).triggerHandler("resize");
        $(tabs).click(function () {
            $(".function ul li").removeClass("cur");
            $($(".function ul li")[$(this).index()]).addClass("cur");
            $(".body ul").removeClass("cur");
            $($(".body ul")[$(this).index()]).addClass("cur");
        });
        $(".btn_new").mousemove(function () {
            var length = $(".body ul").length;
            for (var i = 0; i < length; i++) {
                if ($($(".body ul")[i]).find(".field-validation-error").length > 0) {
                    $($(".function ul li")[i]).click();
                    return;
                }
            }
        });
    }
    else if (MVC.Action == "profession") {
        var tabs = $(".function ul li");
        $(tabs).click(function () {
            $(".function ul li").removeClass("cur");
            $($(".function ul li")[$(this).index()]).addClass("cur");
            $(".body iframe").attr("src", $(this).attr("src"));
        }).first().click();
        $(window).resize(function () {
            var height = document.documentElement.clientHeight;
            $(".body").height(height - 36);
            $(".form").height(height - 36);
            $(".tabview").height(height - 36);
            $(".body").height(height - 36);
        }).triggerHandler("resize");
    }
    else if (MVC.Action == "resume") {
        var tabs = $(".function ul li");        
        $(tabs).click(function () {
            $(".function ul li").removeClass("cur");
            $($(".function ul li")[$(this).index()]).addClass("cur");
            $(".body iframe").attr("src", $(this).attr("src"));
        }).first().click();
        $(window).resize(function () {
            var height = document.documentElement.clientHeight;
            $(".body").height(height - 36);
            $(".form").height(height - 36);
            $(".tabview").height(height - 36);
            $(".body").height(height - 36);
        }).triggerHandler("resize");
    }
    else if (MVC.Action == "indextree") {
        $(".treeview a").click(function () {
            $(".treeview a").removeClass("curSelectedNode");
            $(this).addClass("curSelectedNode");
            var openurl = $(this).attr("openurl");
            if (openurl) {
                treedetail(openurl);
            }
            return false;
        });
        openWin = true;
        $(".index").append($("<div class='split'></div><div class='detail'><iframe id='fmdetail' name='fmdetail' frameborder='0'></iframe></div><div class='mask'></div>"));
        var _move = false;
        var _x, x;
        var leftstart, lw, rw;
        var lnw = 0;
        if ($.cookie("index_lnw") != null) {
            lnw = parseInt($.cookie("index_lnw"));
        }
        var rnw = 0;
        var minlnw = 120;
        var minrnw = 150;
        var draglw = 120;
        var dragrw = 150;
        if (lnw == 0) {
            if ($.browser.msie && $.browser.version <= 7) {
                $(".form").width(document.documentElement.clientWidth * 0.2 - 5);
            }
            else {
                $(".form").width(document.documentElement.clientWidth * 0.2 - 6);
            }
            $(".detail").width(document.documentElement.clientWidth * 0.8);
            lnw = $(".form").width();
            $.cookie("index_lnw", lnw);
        } else {
            $(".form").width(lnw);
            $(".detail").width(document.documentElement.clientWidth - 6 - lnw);
        }

        $(".split").mousedown(function (e) {
            _move = true;
            _x = e.pageX
            leftstart = $("#listCenter").css("left");
            lw = $(".form").width();
            rw = $(".detail").width();
            $(".split").fadeTo(20, 0.5);
            $(".mask").show();
        });
        $(document).mousemove(function (e) {
            if (_move) {
                x = e.pageX - _x;
                if (e.pageX > draglw && e.pageX + dragrw < document.documentElement.clientWidth) {
                    $(".split").css("left", leftstart + x);
                    $(".form").width(lw + x);
                    $(".detail").width(rw - x);
                }
            }
        }).mouseup(function () {
            if (_move) {
                lnw = $(".form").width();
                $.cookie("index_lnw", lnw);
            }
            _move = false;
            $(".split").fadeTo("fast", 1);
            $(".mask").hide();
        });
        $(".form").show();
        $(window).resize(function () {
            if (lnw == 0) {
                lnw = parseInt($.cookie("index_lnw"));
            }
            var height = document.documentElement.clientHeight;
            var width = document.documentElement.clientWidth;
            if (width - 6 >= lnw + minrnw) {
                $(".detail").width(width - lnw - 6);
                $(".form").width(lnw);
                $(".index").width(width);
            } else if (width - 6 >= minlnw + minrnw) {
                $(".detail").width(minrnw);
                $(".index").width(width);
                $(".form").width(width - minrnw - 6);
            } else if (width - 6 < minlnw + minrnw) {
                $(".index").width(minlnw + minrnw + 6);
                $(".detail").width(minrnw);
                $(".form").width(minlnw);
            }
            $(".detail").height(height - 1);
            $(".form").height(height - 1);
            $(".treeview").height(height - 10);
            $("#fmdetail").height(height - 1);
            $(".split").height(height - 1);
            $(".mask").height(height - 1);
        }).triggerHandler("resize");
        $(".switch").click(function () {
            var nclass = $(this).attr("class");
            if (nclass.indexOf("_open") > 0) {
                $(this).attr("class", nclass.replace("_open", "_close"));
                var btn = $(this).parent().children("a").children().first();
                btn.attr("class", btn.attr("class").replace("_open", "_close"));
                $(this).parent().children("ul").hide();
            }
            else {
                $(this).attr("class", nclass.replace("_close", "_open"));
                var btn = $(this).parent().children("a").children().first();
                btn.attr("class", btn.attr("class").replace("_close", "_open"));
                $(this).parent().children("ul").show();
            }
        });
        $(".treeview a:first").click();
    }
   
});
var cur_item = null;
 

function detail(id) {
    $("#fmdetail").attr("src", MVC.DetailUrl + "/" + id);
}

function treedetail(url) {
    $("#fmdetail").attr("src", url);
}

function page_check(v, m) {
    var iv = parseInt(v);
    if (isNaN(iv) || iv <= 0 || iv > m) {
        alert("请输入正确页码");
        return false;
    }
    location.href = MVC.IndexUrl + "&p=" + (iv - 1);
}

function del_item(id) {
    if (!confirm("确定删除？")) {
        return false;
    }
    $("#mvc_command").val("delete");
    $("#mvc_args").val(id);
    SubmitForm();
    return false;
}

function SubmitForm() {
    $("form").submit();
}


function OpenWindow(url, feature, callback) {
    if (openWin) {
        if (url.indexOf('?') > 0) url += "&nc=" + Math.random();
        else url += "?nc=" + Math.random();

        var f = { width: 400, height: 360, status: 0, toolbar: 0, menubar: 0, location: 0, resizable: 1, scrollbars: 1, titlebar: 0, target: "_blank" };
        if (feature) {
            if (!isNaN(parseInt(feature.width))) f.width = parseInt(feature.width);
            if (!isNaN(parseInt(feature.height))) f.height = parseInt(feature.height);
            if (!isNaN(parseInt(feature.left))) f.left = parseInt(feature.left);
            if (!isNaN(parseInt(feature.top))) f.top = parseInt(feature.top);
            if (feature.status) f.status = 1;
            if (feature.toolbar) f.toolbar = 1;
            if (feature.menubar) f.menubar = 1;
            if (feature.location) f.location = 1;
            if (typeof (feature.resizable) == "undefine") f.resizable = 1;
            if (feature.scrollbars) f.scrollbars = 1;
            if (feature.titlebar) f.titlebar = 1;
            if (feature.target) f.target = feature.target;
        }
        if (typeof (f.left) === 'undefined') f.left = Math.max((screen.width - f.width) / 2, 0);
        if (typeof (f.top) === 'undefined') f.top = Math.max((screen.height - f.height - 60) / 2, 0);

        var sFeature = "width=" + f.width + "px,height=" + f.height + "px,left=" + f.left + "px,top=" + f.top + "px,status=" + f.status + ",resizable=" + f.resizable + ",titlebar=" + f.titlebar + ",toolbar=" + f.toolbar + ",scrollbars=" + f.scrollbars + ",menubar=" + f.menubar + ",location=" + f.location;

        var newWin = window.open("", f.target, sFeature);
        try {
            //newWin.document.write("<html><head><title>正在加载...</title></head><body style='font-size: 12px;'>加载中，请稍候……</body></html>")
            newWin.focus();
            newWin.location.href = url;

            if (callback) {
                var pCloseCheck = setInterval(function () {
                    try {
                        if (!newWin || newWin.closed) {
                            try {
                                callback(window._context, window._returnVal);
                            } catch (e) {
                                alert(e.message);
                            }
                            clearInterval(pCloseCheck);
                        }
                    } catch (e) { }
                }, 200);
            }

            return newWin;
        } catch (e) {
            return null;
        }
    }
    else {
        //        if ($("#w_dialogframe")) {
        //            $("#w_dialogframe").remove();
        //        }
        //        var f = { width: 400, height: 360 };
        //        if (feature) {
        //            if (!isNaN(parseInt(feature.width))) f.width = parseInt(feature.width);
        //            if (!isNaN(parseInt(feature.height))) f.height = parseInt(feature.height);
        //        }
        //        var div = $("<div></div>");
        //        div.attr("id", "w_dialogframe").hide();
        //        div.prepend("<iframe src='" + url + "' frameborder='0' scrolling='auto' width='100%' name='w_iframeFrom' id='w_iframeFrom' height='100%'></iframe>");
        //        div.appendTo($("body"));
        //        div.attr("width", f.width).attr("height", f.height);
        //        $("#w_dialogframe").dialog({
        //            autoOpen: true,
        //            height: f.height,
        //            width: f.width,
        //            modal: true,
        //            close: function () {
        //                $("#w_dialogframe").remove();
        //            }
        //        });
        //        $('#w_dialogframe').dialog('open');
        window.top.OpenFrame(url, feature);
    }
}

function OpenFrame(url, feature) {
    if (window.top.OpenUIDialog) {
        window.top.OpenUIDialog(url, feature.width, feature.height);
    }
    else {
        if ($("#w_dialogframe")) {
            $("#w_dialogframe").remove();
        }
        var f = { width: 400, height: 360 };
        if (feature) {
            if (!isNaN(parseInt(feature.width))) f.width = parseInt(feature.width);
            if (!isNaN(parseInt(feature.height))) f.height = parseInt(feature.height);
        }
        var div = $("<div></div>");
        div.attr("id", "w_dialogframe").hide();
        div.prepend("<iframe src='" + url + "' frameborder='0' scrolling='auto' width='100%' name='w_iframeFrom' id='w_iframeFrom' height='100%'></iframe>");
        div.appendTo($("body"));
        div.attr("width", f.width).attr("height", f.height);
        $("#w_dialogframe").dialog({
            autoOpen: true,
            height: f.height,
            width: f.width,
            modal: true,
            close: function () {
                $("#w_dialogframe").remove();
            }
        });
        $('#w_dialogframe').dialog('open');
    }
}

function selectUser(obj) {
    var pageURL = MVC.SelectUrl + "?SelectType=User&nocache=" + Math.random() * 10000 + "&ReturnValueFormat=" + encodeURI(obj.id + ":DisplayName|" + obj.id + "name:Name|" + obj.id + "id:ID")
    var returnValue = window.showModalDialog(pageURL, document, "dialogWidth:800px;dialogHeight:600px;status:yes;resizable:no");
}

function selectDept(obj) {
    var pageURL = MVC.SelectUrl + "?SelectType=Department&nocache=" + Math.random() * 10000 + "&ReturnValueFormat=" + encodeURI(obj.id + ":DisplayName|" + obj.id + "name:Name|" + obj.id + "id:ID")
    var returnValue = window.showModalDialog(pageURL, document, "dialogWidth:800px;dialogHeight:600px;status:yes;resizable:no");
}

function CloseWindow() {
    if (openWin) {
        opener.document.location.href = opener.document.location.href;
        top.opener = null;
        window.open('', '_self');
        window.close();
    }
    else {
        window.parent.location.href = window.parent.location.href;
    }
}

